﻿require.config({
  paths: { "text": "durandal/amd/text" }
});

define([
  'durandal/app',
  'durandal/viewLocator',
  'durandal/system',
  'durandal/plugins/router',
  'services/logger',
  'services/platform',
  'services/appModel'
],
  function (app, viewLocator, system, router, logger, platform, appModel) {

    // Enable debug message to show in the console 
    system.debug(true);

    if (window.PhoneGap) {
      logger.log('running on PhoneGap', 'main');
      document.addEventListener("deviceready", _onDeviceReady, false);
    } else {
      //$(document).ready(function () {
      logger.log('running in Browser', 'main');
      _onDeviceReady();
      //});
    } //if (window.PhoneGap)

    function _onDeviceReady() {
      app.start().then(function () {
        app.title = "onYOURway";

        toastr.options.positionClass = 'toast-bottom-right';
        toastr.options.backgroundpositionClass = 'toast-bottom-right';
        toastr.options.iconClasses = {
          error: 'label-important',
          info: 'label-info',
          success: 'label-success',
          warning: 'label-warning'
        };

        router.handleInvalidRoute = function (route, params) {
          logger.logError('No Route Found', 'main', route);
        };

        // When finding a viewmodel module, replace the viewmodel string 
        // with view to find its partner view.
        router.useConvention();
        viewLocator.useConvention();

        // Adapt to touch devices
        app.adaptToDevice();

        //choose shell depending on environment
        switch (platform.device.type) {
          case 'Phone':
            logger.log('starting Phone shell', 'main');
            //Show the app by setting the root view model for our application.
            //app.setRoot('viewmodels/shellPhone', 'entrance');
            app.setRoot('viewmodels/_shell', 'entrance');
            break;
          //case 'Tablet':
          //case 'PC':
          default:
            logger.log('starting default shell on device type: ' + platform.device.type, 'main');
            //Show the app by setting the root view model for our application.
            app.setRoot('viewmodels/_shell', 'entrance');
            break;
        } //switch (platform.device.type)
        appModel.initializeMap('map');

      }); //app.start().then
    } //_onDeviceReady

    //add ko binding for return key
    ko.bindingHandlers.returnKey = {
      init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
        ko.utils.registerEventHandler(element, 'keydown', function (evt) {
          if (evt.keyCode === 13) {
            evt.preventDefault();
            evt.target.blur();
            //var value = valueAccessor(),
            //    allBindings = allBindingsAccessor();
            //value(allBindings.param);
            valueAccessor().call(viewModel);
          }
        });
      }
    };
    //add ko binding for Date using moment.js
    ko.bindingHandlers.textDate = {
      update: function (element, valueAccessor, allBindingsAccessor, viewModel) {
        var value = valueAccessor(),
            allBindings = allBindingsAccessor();
        var valueUnwrapped = ko.utils.unwrapObservable(value);
        var format = allBindings.format || 'DD.MM.YYYY HH:mm';
        $(element).text(moment(valueUnwrapped).format(format));
      }
    };

  }); //define

